package com.newly.center.sys.service.dict.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.newly.common.base.entity.PageUtils;
import com.newly.common.base.entity.sys.dict.po.DictPo;
import com.newly.common.base.entity.sys.dict.po.DictTypePo;
import com.newly.common.base.exception.NewlyDBException;
import com.newly.common.mapper.sys.DictMapper;
import com.newly.common.mapper.sys.DictTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author gjm guan
 * @date 2021/11/10 15:22
 * @desc 字典管理业务基类
 */
public abstract class AbstractDictService {

    @Autowired
    protected DictMapper dictMapper;

    @Autowired
    protected DictTypeMapper dictTypeMapper;

    /**
     * 修改字典
     *
     * @param dictPo 源字典数据
     * @return 修改成功返回true
     */
    protected boolean updateDict(DictPo dictPo) {
        int result = 0;
        try {
            result = dictMapper.updateById(dictPo);
        } catch (Exception e) {
            throw new NewlyDBException("修改库字典失败: " + e);
        }
        return result > 0;
    }

    /**
     * 修改字典
     *
     * @param dictPo  源字典数据
     * @param wrapper 修改条件
     * @return 修改成功返回true
     */
    protected boolean updateDict(DictPo dictPo, LambdaQueryWrapper wrapper) {
        int result = 0;
        try {
            result = dictMapper.update(dictPo, wrapper);
        } catch (Exception e) {
            throw new NewlyDBException("修改库字典失败: " + e);
        }
        return result > 0;
    }

    /**
     * 通过wrapper查询字典
     *
     * @param wrapper 查询条件
     * @return List DictPo.class
     */
    protected List<DictPo> selectDict(LambdaQueryWrapper<DictPo> wrapper) {
        List<DictPo> dictPos = null;
        try {
            dictPos = dictMapper.selectList(wrapper);
        } catch (Exception e) {
            throw new NewlyDBException("查询库中字典异常");
        }
        return dictPos;
    }

    /**
     * 通过wrapper查询字典,分页查询
     *
     * @param wrapper 查询条件
     * @param page    分页条件
     * @return List DictPo.class
     */
    protected PageUtils selectDict(IPage<DictPo> page, LambdaQueryWrapper<DictPo> wrapper) {
        IPage<DictPo> dictPage = null;
        page = ObjectUtil.isEmpty(page) ? new Page<DictPo>() : page;
        try {
            dictPage = dictMapper.selectPage(page, wrapper);
        } catch (Exception e) {
            throw new NewlyDBException("查询库中字典异常: ", e);
        }
        return new PageUtils(dictPage);
    }

    /**
     * 通过wrapper删除字典
     *
     * @param wrapper 删除条件
     * @return 删除成功返回true
     */
    protected boolean deleteDict(LambdaQueryWrapper<DictPo> wrapper) {
        int result = 0;
        try {
            result = dictMapper.delete(wrapper);
        } catch (Exception e) {
            throw new NewlyDBException("删除库字典失败：" + e);
        }
        return result > 0;
    }

    /**
     * 通过id删除字典
     *
     * @param dictIds 字典id
     * @return 删除成功返回true
     */
    protected boolean deleteDict(List<String> dictIds) {
        if (ObjectUtil.isEmpty(dictIds)) {
            return false;
        }
        int result = 0;
        try {
            result = dictMapper.delete(
                    Wrappers.lambdaUpdate(DictPo.class)
                            .notIn(DictPo::getType, 0, 1, 2)
                            .in(DictPo::getId, dictIds)
            );
        } catch (Exception e) {
            throw new NewlyDBException("删除参数配置失败：" + e);
        }
        return result > 0;
    }

    /**
     * 通过wrapper查询字典类型,分页查询
     *
     * @param wrapper 查询条件
     * @return List DictTypePo.class
     */
    protected List<DictTypePo> selectDictType(LambdaQueryWrapper<DictTypePo> wrapper) {
        List<DictTypePo> dictTypePos = null;
        try {
            dictTypePos = dictTypeMapper.selectList(wrapper);
        } catch (Exception e) {
            throw new NewlyDBException("查询库字典类型失败: " + e);
        }
        return dictTypePos;
    }

    /**
     * 通过wrapper查询字典类型
     *
     * @param wrapper 查询条件
     * @param page    分页对象
     * @return List DictTypePo.class
     */
    protected PageUtils selectDictType(IPage<DictTypePo> page, LambdaQueryWrapper<DictTypePo> wrapper) {
        IPage<DictTypePo> dictTypePoPage = null;
        page = ObjectUtil.isEmpty(page) ? new Page<DictTypePo>() : page;
        try {
            dictTypePoPage = dictTypeMapper.selectPage(page, wrapper);
        } catch (Exception e) {
            throw new NewlyDBException("查询库字典类型失败: " + e);
        }
        return new PageUtils(dictTypePoPage);
    }

    /**
     * 通过id删除字典类型
     *
     * @param dictTypeIds 字典类型id
     * @return 删除成功返回true
     */
    protected boolean deleteDictType(List<String> dictTypeIds) {
        if (ObjectUtil.isEmpty(dictTypeIds)) {
            return false;
        }
        int result = 0;
        try {
            result = dictTypeMapper.deleteBatchIds(dictTypeIds);
        } catch (Exception e) {
            throw new NewlyDBException("删除库字典类型失败: " + e);
        }
        return result > 0;
    }

    /**
     * 通过id删除字典类型
     *
     * @param wrapper 删除条件
     * @return 删除成功返回true
     */
    protected boolean deleteDictType(LambdaQueryWrapper wrapper) {
        int result = 0;
        try {
            result = dictTypeMapper.delete(wrapper);
        } catch (Exception e) {
            throw new NewlyDBException("删除库字典类型失败: " + e);
        }
        return result > 0;
    }

    /**
     * 修改字典类型
     *
     * @param dictTypePo 字典类型数据
     * @return 修改成功返回true
     */
    protected boolean updateDictType(DictTypePo dictTypePo) {
        int result = 0;
        try {
            result = dictTypeMapper.insert(dictTypePo);
        } catch (Exception e) {
            throw new NewlyDBException("修改库字典类型失败: " + e);
        }
        return result > 0;
    }

}
